package com.ficus.road.maintain.third.zh;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.AllFactoryTypeEnum;
import com.ficus.road.maintain.core.model.Hit;
import com.ficus.road.maintain.core.model.zh.BridgeDiseaseZH;
import com.ficus.road.maintain.dao.HitAllFactoryDao;
import com.ficus.road.maintain.strategy.allfactory.AllFactoryContext;
import com.ficus.road.maintain.strategy.allfactory.BridgeInspectionStrategy;
import com.ficus.road.maintain.util.MyDateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/5/20 下午4:19
 */
@Component
public class ZHBridgeDisease extends BaseZH {

    @Autowired
    private HitAllFactoryDao hitAllFactoryDao;

    @PostConstruct
    void init() {
        arg0 = "sanwangBridgeDisease";
        arg1 = "{}";
        errMsg = "调用中海接口获取PCI和PQI异常!";
        successMsg = "调用中海接口获取PCI和PQI成功!";
    }

    @Scheduled(cron = "0 0 12,14,16 * * ?")
//    @Scheduled(cron = "0/5 * * * * ?")
    public void refreshhBridgeDisease() {
//        initHistoryData();
        init();
        Date today = new Date();
        String endDateStr = MyDateUtil.dateFormatStr(today, "yyyy-MM-dd");
        String startDateStr = MyDateUtil.dateFormatStr(today, "yyyy-MM-dd");
        arg1 = String.format("{\"diseasestatus\":\"1\",\"startTime\":\"%s\",\"endTime\":\"%s\"}",
                startDateStr, endDateStr);
        String bridgeDiseaseStr = getZHData();
        if (StringUtils.isEmpty(bridgeDiseaseStr)) {
            return;
        }
        List<BridgeDiseaseZH> bridgeDiseaseZHList = JSONArray.parseArray(bridgeDiseaseStr, BridgeDiseaseZH.class);
        AllFactoryContext allFactoryContext = new AllFactoryContext(new BridgeInspectionStrategy(hitAllFactoryDao));
        bridgeDiseaseZHList.forEach(zh -> {
            Hit hit = allFactoryContext.convert2AllFactoryHit(zh);
            hitAllFactoryDao.saveOrUpdate(hit);
        });
    }

    private void initHistoryData() {
        List<Hit> hits = hitAllFactoryDao.findByQuery(new Query(Criteria.where("type").is(AllFactoryTypeEnum.BRIDGE_INSPECTION)));
        hits.parallelStream().forEach(hit -> {
            hitAllFactoryDao.deleteById(hit.getId());
        });
        Date endDate = new Date();
        for (int i = 0; i < 12; i++) {
            Date startDate = DateUtils.addDays(endDate, -30);
            String endDateStr = MyDateUtil.dateFormatStr(endDate, "yyyy-MM-dd");
            String startDateStr = MyDateUtil.dateFormatStr(startDate, "yyyy-MM-dd");
            arg1 = String.format("{\"diseasestatus\":\"1\",\"startTime\":\"%s\",\"endTime\":\"%s\"}",
                    startDateStr, endDateStr);
            String bridgeDiseaseStr = getZHData();
            if (StringUtils.isEmpty(bridgeDiseaseStr)) {
                return;
            }
            List<BridgeDiseaseZH> bridgeDiseaseZHList = JSONArray.parseArray(bridgeDiseaseStr, BridgeDiseaseZH.class);
            AllFactoryContext allFactoryContext = new AllFactoryContext(new BridgeInspectionStrategy(hitAllFactoryDao));
            bridgeDiseaseZHList.forEach(zh -> {
                Hit hit = allFactoryContext.convert2AllFactoryHit(zh);
                hitAllFactoryDao.saveOrUpdate(hit);
            });
            endDate = startDate;
        }
    }
}
